<?php

namespace App\Models\Finance;

use App\Models\CustomerManager;
use App\Models\ShopDetail;
use App\Models\SubAccount\SubAccount;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class FinanceCustomerBill extends Model
{

    protected $guarded=[];
    protected $appends=['bill_shops'];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y/m/d H:i:s');
    }

    public function customer(){
        return $this->belongsTo(CustomerManager::class,'customer_id');
    }

    public function accountBills(){
        return $this->hasMany(FinanceShopSubAccountBill::class,'bill_id');
    }

    public function billShops(): Attribute{
        return Attribute::make(
            get: fn ($value) => $this->getShopNames(),
        );
    }
    //获取子账号账单店铺名称
    public function getShopNames(){
        $names='';
        $shopNames=$this->accountBills()->pluck('shop_name')->toArray();
        if(!empty($shopNames)){
            $shopNames=array_unique($shopNames);
            $names=implode('，',$shopNames);
        }
        return $names;
    }
    //
    public function branchBills(){
        return $this->hasMany(FinanceBranchBill::class,'customer_bill_id');
    }


}
